package com.example.day03.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.day03.entity.ReginLoginEntity
import com.example.day03.entity.Res
import com.example.day03.mvvm.BaseViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody

class LoginViewModel:BaseViewModel() {
    val loginsuccess=MutableLiveData<Res<ReginLoginEntity>>()
    val loginfail=MutableLiveData<String>()
    fun getLogin(phone:String,pass:String){
        viewModelScope.launch {
            val map=HashMap<String,String>()
            map["account"]=phone
            map["password"]=pass
            val json = Gson().toJson(map)
            val body = RequestBody.create("application/json;charset=utf-8".toMediaType(), json)
            loginsuccess.postValue(apiService.getLogin(body))
        }
    }
}